#include "CountDownLatch.h"

using namespace _zhy_muduo;

CountDownLatch::CountDownLatch(const int count)
	:mutex_(),
	condition_(mutex_),
	count_(count)
{
}

void CountDownLatch::wait()
{
	MutexLockGuard lock(mutex_);
	while (count_ > 0) {
		condition_.wait();
	}
}

void CountDownLatch::countDown()
{
	MutexLockGuard lock(mutex_);
	--count_;
	if (0 == count_) {
		condition_.notifyAll();
	}
}

int CountDownLatch::getCount() const
{
	MutexLockGuard lock(mutex_);
	return count_;
}
